<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>预览</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
    <style>
        :root {
            --primary: #6366f1;
            --secondary: #a855f7;
            --radius: 12px;
            --shadow: 0 8px 20px rgba(0, 0, 0, .08);
        }
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Helvetica, "PingFang SC", "Microsoft YaHei", sans-serif;
            background: #f7f9fc;
            color: #333;
            line-height: 1.6;
            -webkit-text-size-adjust: 100%; /* 防止iOS设备字体缩放 */
        }
        .hero {
            min-height: 100vh;
            background: linear-gradient(rgba(0, 0, 0, .4), rgba(0, 0, 0, .4)) center/cover;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            text-align: center;
            color: #fff;
            padding: 20px;
            position: relative;
        }
        #audio-element {
            display: none;
        }
        .play-button {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
            border: none;
            padding: 16px 32px;
            border-radius: 50px;
            font-size: 18px;
            cursor: pointer;
            margin-top: 20px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
            min-height: 56px; /* 触摸友好尺寸 */
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .play-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
        }
        .play-button:active {
            transform: translateY(0);
        }
        .audio-tip {
            margin-top: 10px;
            font-size: 14px;
            opacity: 0.8;
        }
        .hero h1 {
            font-size: clamp(2rem, 8vw, 3.5rem);
            font-weight: 700;
            letter-spacing: 1px;
            animation: fadeInDown 1s both;
            margin-bottom: 15px;
            line-height: 1.2;
        }
        .hero p {
            font-size: clamp(1rem, 4vw, 1.3rem);
            margin: 15px 0 25px;
            animation: fadeInUp 1s .3s both;
            line-height: 1.4;
        }
        .scroll-tip {
            position: absolute;
            bottom: 25px;
            animation: bounce 2s infinite;
            font-size: 24px;
        }
        section {
            padding: 40px 20px;
            max-width: 900px;
            margin: 0 auto;
        }
        .title {
            text-align: center;
            font-size: 1.8rem;
            margin-bottom: 30px;
            background: linear-gradient(90deg, var(--primary), var(--secondary));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .card {
            background: #fff;
            border-radius: var(--radius);
            overflow: hidden;
            box-shadow: var(--shadow);
            margin-bottom: 30px;
            display: flex;
            flex-direction: column;
        }
        .txt {
            padding: 20px;
        }
        .txt h3 {
            font-size: 1.3rem;
            margin-bottom: 10px;
            line-height: 1.3;
        }
        .txt p {
            color: #666;
            line-height: 1.5;
        }

        /* 滑动容器样式优化 */
        .swipe-container {
            position: relative;
            width: 100%;
            overflow: hidden;
        }
        .swipe {
            display: flex;
            overflow-x: auto;
            scroll-snap-type: x mandatory;
            -webkit-overflow-scrolling: touch;
            scrollbar-width: none; /* Firefox */
            -ms-overflow-style: none; /* IE and Edge */
            padding: 10px 0;
        }
        .swipe::-webkit-scrollbar {
            display: none; /* Chrome, Safari and Opera */
        }
        .swipe img,
        .swipe video {
            flex: 0 0 auto;
            width: 85%;
            scroll-snap-align: center;
            margin: 0 10px;
            border-radius: 8px;
            max-height: 180px;
            object-fit: cover;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .swipe-indicator {
            display: flex;
            justify-content: center;
            margin-top: 10px;
        }
        .swipe-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #ddd;
            margin: 0 4px;
            transition: background 0.3s;
        }
        .swipe-dot.active {
            background: var(--primary);
        }

        /* 移动端优化 */
        @media (max-width: 599px) {
            .hero {
                padding: 20px 15px;
                min-height: 90vh; /* 避免在短屏设备上过度滚动 */
            }
            .hero h1 {
                font-size: 2.2rem;
            }
            .hero p {
                font-size: 1.1rem;
            }
            .play-button {
                padding: 14px 28px;
                font-size: 16px;
                min-height: 50px;
                width: 100%;
                max-width: 280px;
            }
            section {
                padding: 30px 15px;
            }
            .card {
                margin-bottom: 25px;
            }
            .card img,
            .card video {
                max-height: 160px;
            }
            .txt {
                padding: 15px;
            }
            .txt h3 {
                font-size: 1.2rem;
            }
            .swipe img,
            .swipe video {
                width: 80%;
                max-height: 150px;
            }
        }

        /* 平板和桌面端 */
        @media (min-width: 600px) {
            .card {
                flex-direction: row;
                margin-bottom: 40px;
            }
            .card img,
            .card video {
                width: 50%;
                height: auto;
                max-height: 200px;
            }
            .txt {
                width: 50%;
                display: flex;
                flex-direction: column;
                justify-content: center;
            }
            .swipe img,
            .swipe video {
                width: 40%;
                max-height: 200px;
            }
        }

        /* 大屏设备 */
        @media (min-width: 1024px) {
            .hero h1 {
                font-size: 3.5rem;
            }
            .hero p {
                font-size: 1.4rem;
            }
            .card img,
            .card video {
                max-height: 250px;
            }
            .swipe img,
            .swipe video {
                max-height: 220px;
            }
        }

        @keyframes fadeInDown {
            from {
                opacity: 0;
                transform: translateY(-30px)
            }
            to {
                opacity: 1;
                transform: translateY(0)
            }
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px)
            }
            to {
                opacity: 1;
                transform: translateY(0)
            }
        }
        @keyframes bounce {
            0%,
            100% {
                transform: translateY(0)
            }
            50% {
                transform: translateY(-10px)
            }
        }
    </style>
</head>

<body>

<!-- 首屏 -->
<div class="hero" id="hero">
    <h1 id="heroTitle"></h1>
    <p id="heroSub"></p>

    <!-- 播放按钮 -->
    <button class="play-button" id="playButton">
        🔊 播放语音介绍
    </button>
    <div class="audio-tip" id="audioTip">点击按钮播放语音</div>

    <div class="scroll-tip">↓</div>
    <audio id="audio-element"></audio>
</div>

<!-- 模块容器 -->
<section id="content"></section>

<script th:inline="javascript">
    /* ===== 1. 后端注入 ===== */
    window.CONFIG = /*[[${configJson}]]*/ {};

    // 全局变量
    let audioElement = null;
    let audioUrl = null;
    let isAudioLoaded = false;

    // 页面加载后初始化
    window.addEventListener('DOMContentLoaded', function() {
        audioElement = document.getElementById('audio-element');
        const playButton = document.getElementById('playButton');
        const audioTip = document.getElementById('audioTip');

        // 预加载音频（但不自动播放）
        loadAudio();

        // 播放按钮点击事件
        playButton.addEventListener('click', function() {
            playAudio();
        });

        // 整个英雄区域点击也可以播放（增强用户体验）
        document.getElementById('hero').addEventListener('click', function(e) {
            // 防止重复触发
            if (e.target !== playButton) {
                playAudio();
            }
        });

        // 键盘空格键播放
        document.addEventListener('keydown', function(e) {
            if (e.code === 'Space') {
                e.preventDefault();
                playAudio();
            }
        });
    });


    window.CONFIG = JSON.parse(window.CONFIG);

    console.log(window.CONFIG.hero.id)
    // console.log(window.CONFIG.hero)

    // 加载音频函数
    function loadAudio() {
        const audioTip = document.getElementById('audioTip');

        fetch('http://192.168.10.3:8085/api/tts?id=' + window.CONFIG.hero.id, {
            method: 'POST'
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络响应不正常');
                }
                return response.blob();
            })
            .then(blob => {
                audioUrl = URL.createObjectURL(blob);
                audioElement.src = audioUrl;

                // 音频加载事件
                audioElement.addEventListener('canplaythrough', function() {
                    isAudioLoaded = true;
                    audioTip.textContent = '音频加载完成，点击播放';
                    document.getElementById('playButton').textContent = '🔊 播放语音介绍';
                });

                // 音频错误事件
                audioElement.addEventListener('error', function() {
                    audioTip.textContent = '音频加载失败';
                    document.getElementById('playButton').textContent = '❌ 加载失败';
                });
            })
            .catch(error => {
                console.error('语音合成失败:', error);
                audioTip.textContent = '语音合成失败: ' + error.message;
                document.getElementById('playButton').textContent = '❌ 加载失败';
            });
    }

    // 播放音频函数
    function playAudio() {
        const audioTip = document.getElementById('audioTip');
        const playButton = document.getElementById('playButton');

        if (!isAudioLoaded) {
            audioTip.textContent = '音频正在加载中，请稍候...';
            return;
        }

        if (audioElement.paused) {
            // 播放音频
            audioElement.play().then(() => {
                audioTip.textContent = '播放中...';
                playButton.textContent = '⏸️ 暂停播放';
            }).catch(error => {
                console.error('播放失败:', error);
                audioTip.textContent = '播放失败，请点击重试';
            });
        } else {
            // 暂停音频
            audioElement.pause();
            audioTip.textContent = '已暂停，点击继续播放';
            playButton.textContent = '🔊 继续播放';
        }

        // 监听音频结束
        audioElement.onended = function() {
            audioTip.textContent = '播放完成';
            playButton.textContent = '🔊 重新播放';
        };
    }

    /* ===== 2. 渲染页面内容 ===== */
    console.log(window.CONFIG);


    const h = window.CONFIG.hero;
    document.getElementById('heroTitle').textContent = h.title;
    document.getElementById('heroSub').textContent = h.subtitle;
    document.querySelector('.hero').style.backgroundImage = `linear-gradient(rgba(0,0,0,.4),rgba(0,0,0,.4)), url(${h.bg})`;

    const box = document.getElementById('content');
    (window.CONFIG.modules || []).forEach((m, idx) => {
        const card = document.createElement('div');
        card.className = 'card';
        card.style.animation = `fadeInUp .6s ${idx * .1}s both`;

        if (m.type === 'text') {
            card.innerHTML = `<div class="txt"><h3>旅途札记</h3><p>${m.text}</p></div>`;
        }
        if (m.type === 'image') {
            const urls = Array.isArray(m.list) ? m.list : [m.list];
            const html = urls.map(u => `<img src="${u}" />`).join('');
            const swipeContainer = document.createElement('div');
            swipeContainer.className = 'swipe-container';
            swipeContainer.innerHTML = `
                <div class="swipe">${html}</div>
                <div class="swipe-indicator">${urls.map((_, i) => `<div class="swipe-dot ${i === 0 ? 'active' : ''}"></div>`).join('')}</div>
            `;
            card.innerHTML = `<div class="txt"><h3>精美图集</h3><p>滑动查看更多</p></div>`;
            card.prepend(swipeContainer);

            // 添加滑动指示器逻辑
            const swipe = swipeContainer.querySelector('.swipe');
            const dots = swipeContainer.querySelectorAll('.swipe-dot');
            swipe.addEventListener('scroll', () => {
                const scrollPos = swipe.scrollLeft;
                const itemWidth = swipe.querySelector('img').offsetWidth + 20; // 包含margin
                const activeIndex = Math.round(scrollPos / itemWidth);

                dots.forEach((dot, i) => {
                    dot.classList.toggle('active', i === activeIndex);
                });
            });
        }
        if (m.type === 'video') {
            const urls = Array.isArray(m.list) ? m.list : [m.list];
            const html = urls.map(u => `<video src="${u}" muted controls playsinline></video>`).join('');
            const swipeContainer = document.createElement('div');
            swipeContainer.className = 'swipe-container';
            swipeContainer.innerHTML = `
                <div class="swipe">${html}</div>
                <div class="swipe-indicator">${urls.map((_, i) => `<div class="swipe-dot ${i === 0 ? 'active' : ''}"></div>`).join('')}</div>
            `;
            card.innerHTML = `<div class="txt"><h3>精彩视频</h3><p>左右滑动切换</p></div>`;
            card.prepend(swipeContainer);

            // 添加滑动指示器逻辑
            const swipe = swipeContainer.querySelector('.swipe');
            const dots = swipeContainer.querySelectorAll('.swipe-dot');
            swipe.addEventListener('scroll', () => {
                const scrollPos = swipe.scrollLeft;
                const itemWidth = swipe.querySelector('video').offsetWidth + 20; // 包含margin
                const activeIndex = Math.round(scrollPos / itemWidth);

                dots.forEach((dot, i) => {
                    dot.classList.toggle('active', i === activeIndex);
                });
            });
        }
        box.appendChild(card);
    });
</script>
</body>
</html>